package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.dto.SysRateDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.SysRate;
import com.ruoyi.service.ISysRateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评分Controller
 *
 * @author ruoyi
 * @date 2024-01-05
 */
@RestController
@RequestMapping("/system/rate")
public class SysRateController extends BaseController
{
    @Autowired
    private ISysRateService sysRateService;


    @PostMapping("setRate")
    public AjaxResult setRate(@RequestBody SysRateDto sysRateDto){
        return sysRateService.setRate(sysRateDto);
    }
    @GetMapping("getAvg")
    public AjaxResult getAvg(  String bookId){
        return sysRateService.getAvg(bookId);
    }

    @GetMapping("getPeopleForScore")
    public AjaxResult getPeopleForScore( String bookId){
        return sysRateService.getPeopleForScore(bookId);
    }

    /**
     * 查询评分列表
     */
    @PreAuthorize("@ss.hasPermi('system:rate:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRate sysRate)
    {
        startPage();
        List<SysRate> list = sysRateService.selectSysRateList(sysRate);
        return getDataTable(list);
    }

    /**
     * 导出评分列表
     */
    @PreAuthorize("@ss.hasPermi('system:rate:export')")
    @Log(title = "评分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRate sysRate)
    {
        List<SysRate> list = sysRateService.selectSysRateList(sysRate);
        ExcelUtil<SysRate> util = new ExcelUtil<SysRate>(SysRate.class);
        util.exportExcel(response, list, "评分数据");
    }

    /**
     * 获取评分详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:rate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysRateService.selectSysRateById(id));
    }

    /**
     * 新增评分
     */
    @PreAuthorize("@ss.hasPermi('system:rate:add')")
    @Log(title = "评分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRate sysRate)
    {
        return toAjax(sysRateService.insertSysRate(sysRate));
    }
    /**
     * 修改评分
     */
    @PreAuthorize("@ss.hasPermi('system:rate:edit')")
    @Log(title = "评分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRate sysRate)
    {
        return toAjax(sysRateService.updateSysRate(sysRate));
    }

    /**
     * 删除评分
     */
    @PreAuthorize("@ss.hasPermi('system:rate:remove')")
    @Log(title = "评分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysRateService.deleteSysRateByIds(ids));
    }
}
